Skip to content

Conversation

@LinuxJedi
Copy link
Member

When wolfSSH_SFTP_buffer_send() called wolfSSH_stream_send(), the data would be consumed into the SSH output buffer even if the underlying socket returned EWOULDBLOCK/EAGAIN. SendChannelData() returns the positive dataSz on WS_WANT_WRITE, causing the SFTP layer to advance its buffer index as if the data was sent. The SSH output buffer still had pending data that was never flushed, leading to an indefinite hang.

Fix: At the start of wolfSSH_SFTP_buffer_send(), check if there's pending data in ssh->outputBuffer from a previous WS_WANT_WRITE. If so, attempt to flush it first and return WS_WANT_WRITE if the flush fails. This ensures the caller retries until all pending data is sent.

Also expose WS_SFTP_BUFFER and wolfSSH_SFTP_buffer_send() as WOLFSSH_LOCAL for unit testing, and add regression test that verifies the fix catches the bug.

Fixes ZD 21157

When wolfSSH_SFTP_buffer_send() called wolfSSH_stream_send(), the data
would be consumed into the SSH output buffer even if the underlying
socket returned EWOULDBLOCK/EAGAIN. SendChannelData() returns the
positive dataSz on WS_WANT_WRITE, causing the SFTP layer to advance
its buffer index as if the data was sent. The SSH output buffer still
had pending data that was never flushed, leading to an indefinite hang.

Fix: At the start of wolfSSH_SFTP_buffer_send(), check if there's
pending data in ssh->outputBuffer from a previous WS_WANT_WRITE. If
so, attempt to flush it first and return WS_WANT_WRITE if the flush
fails. This ensures the caller retries until all pending data is sent.

Also expose WS_SFTP_BUFFER and wolfSSH_SFTP_buffer_send() as
WOLFSSH_LOCAL for unit testing, and add regression test that verifies
the fix catches the bug.

Fixes ZD 21157
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Fixes an SFTP server hang in non-blocking mode when the underlying socket returns EWOULDBLOCK/EAGAIN (mapped to WS_WANT_WRITE) by ensuring previously-buffered SSH output is flushed before consuming more SFTP-layer data, and by preventing the SFTP receive state machine from treating buffered-but-unsent data as fully transmitted.

Changes:

  • Add a flush-first step in wolfSSH_SFTP_buffer_send() when ssh->outputBuffer already has pending bytes.
  • Update wolfSSH_SFTP_read() to treat “SFTP buffer fully consumed but SSH output still pending” as WS_WANT_WRITE.
  • Expose WS_SFTP_BUFFER and wolfSSH_SFTP_buffer_send() for unit/regression testing and add CI coverage to detect hangs.

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 2 comments.

File Description
wolfssh/wolfsftp.h Moves WS_SFTP_BUFFER to the header and exposes wolfSSH_SFTP_buffer_send() as WOLFSSH_LOCAL to enable unit testing.
src/wolfsftp.c Flushes pending SSH output before sending new SFTP buffer data; adds a WS_WANT_WRITE guard when SSH still has unsent buffered output.
tests/regress.c Adds a regression test that simulates pending SSH output + WS_WANT_WRITE and verifies the SFTP buffer is not consumed.
.github/workflows/paramiko-sftp-test.yml Enhances Paramiko SFTP workflow with a repeated-GET stress test and hang detection timeout.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@JacobBarthelmeh JacobBarthelmeh merged commit e60aafe into wolfSSL:master Feb 12, 2026
100 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants